#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    try:
        btn=WaitforElementByName(driver,u'我的',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementByName(driver,u'下载队列',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        btn=WaitforElementById(driver,'com.xiaomi.market:id/header_secondary_title',30)
    except NoSuchElementException:
        driver.back()
    else:
        btn.click()

    try:
        tt=WaitforElementById(driver,'android:id/button1',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        tt.click()

    driver.back()

def clickappdown(driver):
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    try:
        btns=WaitforElementsByContentDesc(driver,u'安装',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn in  btns:
            btn.click()
            time.sleep(2)
            btn.click()





def soft(driver):
    categorys = [u'动作冒险',u'休闲益智',u'影音视听',u'实用工具',u'聊天社交',u'图书阅读',u'时尚购物',u'摄影摄像',u'学习教育',u'旅行交通',u'金融理财', \
                 u'娱乐消遣',u'新闻资讯',u'居家生活',u'体育运动',u'医疗健康',u'效率办公']

    # try:
    #     btn=WaitforElementsById(driver,'com.xiaomi.market:id/icon',60)
    # except:
    #     raise NoSuchElementException
    # else:
    #     btn[0].click()

    try:
        btn=WaitforElementByName(driver,u'分类',30)
    except:
        raise NoSuchElementException
    else:
        btn.click()

    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                btn=WaitforElementByName(driver,u"全部",10)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                btn.click()
                try:
                    clickappdown(driver)
                except NoSuchElementException:
                    continue
        driver.back()


def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.xiaomi.market'    # apk的包名
    desired_caps['appActivity'] = '.ui.MarketTabActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    #time.sleep(3)
    #del_appdown_task(driver)
    time.sleep(2)
    soft(driver)
    driver.quit()

run()
